/***************************************
 * @Name : 身份证校验工具方法
 * @ID : Id_card
 * @CreaterName : chenyl
 * @Email : chenyongrong@bjnja.com
 ***************************************/
/**
 * 判断输入的是否身份证号码
 */
export function checkIdCardNo(num) {
	//身份证号不存在，则直接返回错误
	if (!num) {
		return false
	}

	//身份证号码不允许为111111111111111或将111111191111111113.20100817修改
	if (num == '111111111111111' || num == '111111191111111113') {
		return false
	}

	num = num.toUpperCase()
	//身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。
	if (!/(^\d{17}$)|(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num)) {
		return false
	}

	//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
	//下面分别分析出生日期和校验位
	let len, re
	len = num.length
	if (len == 17) {
		re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})$/)
		var arrSplit = num.match(re)
		//检查生日日期是否正确
		var dtmBirth = new Date(
			arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]
		)
		var bGoodDay
		bGoodDay =
			dtmBirth.getFullYear() == Number(arrSplit[2]) &&
			dtmBirth.getMonth() + 1 == Number(arrSplit[3]) &&
			dtmBirth.getDate() == Number(arrSplit[4])
		if (!bGoodDay) {
			return false
		} else {
			//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
			var arrInt = new Array(
				7,
				9,
				10,
				5,
				8,
				4,
				2,
				1,
				6,
				3,
				7,
				9,
				10,
				5,
				8,
				4,
				2
			)
			var arrCh = new Array(
				'1',
				'0',
				'X',
				'9',
				'8',
				'7',
				'6',
				'5',
				'4',
				'3',
				'2'
			)

			var nTemp = 0,
				i
			for (i = 0; i < 17; i++) {
				nTemp += num.substr(i, 1) * arrInt[i]
			}
			//如果只输入17位数字时，最后一位数字可根据公安算法计算出，并赋值于原本item对象
			num += arrCh[nTemp % 11]
			return num
		}
	}

	if (len == 15) {
		re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/)
		var arrSplit = num.match(re)
		//检查生日日期是否正确
		var dtmBirth = new Date(
			'19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]
		)
		var bGoodDay
		bGoodDay =
			dtmBirth.getFullYear() == Number('19' + arrSplit[2]) &&
			dtmBirth.getMonth() + 1 == Number(arrSplit[3]) &&
			dtmBirth.getDate() == Number(arrSplit[4])
		if (!bGoodDay) {
			return false
		} else {
			//15位身份证只能校验生日日期是否符合格式，无校验位可校验
			return num
		}
	}

	if (len == 18) {
		re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/)
		var arrSplit = num.match(re)

		//检查生日日期是否正确
		var dtmBirth = new Date(
			arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]
		)
		var bGoodDay
		bGoodDay =
			dtmBirth.getFullYear() == Number(arrSplit[2]) &&
			dtmBirth.getMonth() + 1 == Number(arrSplit[3]) &&
			dtmBirth.getDate() == Number(arrSplit[4])
		if (!bGoodDay) {
			return false
		} else {
			//检验18位身份证的校验码是否正确。
			//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
			let valnum
			var arrInt = new Array(
				7,
				9,
				10,
				5,
				8,
				4,
				2,
				1,
				6,
				3,
				7,
				9,
				10,
				5,
				8,
				4,
				2
			)
			var arrCh = new Array(
				'1',
				'0',
				'X',
				'9',
				'8',
				'7',
				'6',
				'5',
				'4',
				'3',
				'2'
			)
			var nTemp = 0,
				i
			for (i = 0; i < 17; i++) {
				nTemp += num.substr(i, 1) * arrInt[i]
			}
			valnum = arrCh[nTemp % 11]
			if (valnum != num.substr(17, 1)) {
				return false
			}
			return num
		}
	}
	return false
}

/**
 * 从身份证号码中得到出生日期
 * @param num 身份证号码
 * @returns {String} 出生年月
 */
export function getBirthday(num) {
	let len, re
	len = num.length
	if (len == 15) {
		re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/)
		var arrSplit = num.toUpperCase().match(re)

		//检查生日日期是否正确
		var dtmBirth = new Date(
			'19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]
		)
		var birYear, birMonth, birDate
		birYear = dtmBirth.getFullYear()
		birMonth = dtmBirth.getMonth() + 1
		birDate = dtmBirth.getDate()

		if (birMonth < 10) birMonth = '0' + birMonth
		if (birDate < 10) birDate = '0' + birDate
		return birYear + '-' + birMonth + '-' + birDate
	}
	if (len == 18) {
		re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/)
		var arrSplit = num.toUpperCase().match(re)

		//检查生日日期是否正确
		var dtmBirth = new Date(
			arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]
		)
		var birYear, birMonth, birDate
		birYear = dtmBirth.getFullYear()
		birMonth = dtmBirth.getMonth() + 1
		birDate = dtmBirth.getDate()

		if (birMonth < 10) birMonth = '0' + birMonth
		if (birDate < 10) birDate = '0' + birDate
		return birYear + '-' + birMonth + '-' + birDate
	}
}

/**
 * 从身份证号码中得到性别
 * @param num 身份证号码
 * @returns 性别  1男    2女
 */
export function getSex(num) {
	let len
	len = num.length
	if (len == 15) {
		var xbNum = num.substring(14, 15)
		return xbNum % 2 == 0 ? 'F' : 'M' //四川数据库 男M 女F
		//return xbNum%2==0 ? 'F' : 'M';
	}
	if (len == 18) {
		var xbNum = num.substring(16, 17)
		return xbNum % 2 == 0 ? 'F' : 'M' //四川数据库 男M 女F
		//return xbNum%2==0 ? 'F' : 'M';
	}
}

/**
 * 从身份证号码中得到出身地
 * @param num 身份证号码
 * @returns {String} 省份6位码
 */
export function getBirthPlace(num) {
	let len
	len = num.length
	if (len == 18 || len == 15) {
		return num.substring(0, 2) + '0000'
	}
}
